package com.qxzn.module.home.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.lmc.entity.Health;
import com.lmc.health.HealthReportActivity;
import com.lmc.util.JsonManager;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.NewsMessageBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.banner.BannerHelper;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;
import com.qxzn.module.home.adapter.NewsItemAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NewsActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    private List<NewsMessageBean> newsList = new ArrayList<>();

    @BindView(R2.id.news_recycle_view)
    LRecyclerView newsRecycleView;

    @Override
    protected int initLayout() {
        return R.layout.activity_news;
    }

    @Override
    protected void initView() {
        LRecyclerViewAdapter newsAdapter = new LRecyclerViewAdapter(new NewsItemAdapter(this, newsList));
        newsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                // todo 点击
                NewsMessageBean newsMessageBean = newsList.get(position);
                if (newsMessageBean.type == 4) {
                    JSONObject params = new JSONObject();
                    params.put("device_id", SharePreferenceUtil.getDeviceId(NewsActivity.this));
                    params.put("username", SharePreferenceUtil.getUserName(NewsActivity.this));
                    HttpRequest.getService().getDaily(HttpRequest.getDefaultBody(params)).enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            try {
                                String string = response.body().string();
                                Log.d("-----", string);
                                Health health = JsonManager.parseJson(string, Health.class);

                                if (health != null && health.getData() != null && health.getData().getResult() != null && health.getData().getResult().size() > 0) {
                                    HealthReportActivity.start(NewsActivity.this, health);
                                } else {
                                    ToastUtil.showTextToast(NewsActivity.this, "暂无数据");
                                }
                            } catch (Exception e) {
                                Log.e("-----", e.toString());
                            }

                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            ToastUtil.showTextToast(NewsActivity.this, getString(R.string.network_error));
                        }
                    });

                } else
                    BannerHelper.jumping(NewsActivity.this, newsMessageBean.comment, newsMessageBean.robot_code, newsMessageBean.username);
            }
        });
        newsRecycleView.setOnRefreshListener(this);
        newsRecycleView.setOnLoadMoreListener(this);
        newsRecycleView.setAdapter(newsAdapter);
        newsRecycleView.setLoadMoreEnabled(false);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("消息中心");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        newsRecycleView.refresh();
    }

    @Override
    public void onRefresh() {
        loadData(0);
    }

    @Override
    public void onLoadMore() {
        loadData(1);
    }

    private void loadData(final int page) {
//        newsRecycleView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                newsRecycleView.refreshComplete(0);
//                int i = 5;
//                while (i-- > 0)
//                    if (page == 0)
//                        newsList.add(0, (int) (Math.random() * 3));
//                    else
//                        newsList.add((int) (Math.random() * 3));
//                newsRecycleView.getAdapter().notifyDataSetChanged();
//            }
//        }, 1000);

        JSONObject params = new JSONObject();
        params.put("robot_code", SharePreferenceUtil.getDeviceId(this));
//        params.put("username", SharePreferenceUtil.getUserName(this));
        HttpRequest.getService().getNews(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean<List<NewsMessageBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<List<NewsMessageBean>>> call, Response<BaseBean<List<NewsMessageBean>>> response) {
                newsRecycleView.refreshComplete(0);
                BaseBean<List<NewsMessageBean>> body = response.body();
                if (body.getCode() == 200) {
                    List<NewsMessageBean> data = body.getData();
                    if (data != null && data.size() > 0) {
                        newsList.clear();
                        newsList.addAll(data);
                        newsRecycleView.getAdapter().notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseBean<List<NewsMessageBean>>> call, Throwable t) {
                newsRecycleView.refreshComplete(0);
                ToastUtil.showTextToast(NewsActivity.this, getString(R.string.network_error));
            }
        });
    }
}
